In this lesson we will
walk thru a few built-in commands in DX that allow you to create a
Wallpaper Changer.
We will start simple and add to the script. It is amazing all the
things that are built into DX.
We will use the file dialog box,
the wallpaper changer, create a "right-click" menu and even a custom preference
window.
For this and all the
Step-By-Step DX Tutorials you will need to purchase
DesktopX for $14.95 from Stardock.
Lets get started.
STEP 1 - Using the System.FileOpenDialog & System.SetWallpaper
Create a new object (look at the old
tutorials for info on how to do this).
Once it is created (you can use any
image you want, but for me I'm just going to use the standard round DX image.
Add the following script:
Vbscript
Code
Function Object_OnLButtonUp(x, y, Dragged)
If Dragged = False Then
'--- Change the line below to pick the folder
'--- where you keep your wallpapers -----
FolderName = "C:\wallpapers"
wallpaper = System.FileOpenDialog("Select Wallpaper...", "", FolderName , "Wallpapers|*.jpg", 0)
System.SetWallpaper wallpaper, 3
End If
End Function
Looking at the above script we
have some new things in here.
Vbscript
Code
System.SetWallpaper wallpaper, 3
The FileOpenDialog is a built-in
DesktopX function. It allows you to select a file from a standard windows
looking open dialog box.
The format is very simple.
"Select Wallpaper..." -
Title for the window
"" - default file (say you
want it to come up with a file selected, this would be that file name)
FolderName - The default
folder to open
"Wallpapers |*.jpg" - this
is the file Extension to use, in this case JPG
The way this works, is you click
on the object, it pops open the FileOpenDialog , then you select your wallpaper
this file name gets saved into the Wallpaper var.
Vbscript
Code
System.SetWallpaper wallpaper, 3
This is another built-in DesktopX
command. It does exactly what it says "SetWallpaper". It gets passed
2 items:
wallpaper - the file we
picked from above, has to have the entire path (IE: c:\wallpaper\mywallpaper.jpg
)
3 - There are 3 options for
this: (we will add a right-click menu later to select this)
0 = use default wallpaper
1 = Center wallpaper
2 = Tile wallpaper
3 = Stretch wallpaper
So with just the above script you
have created a object/widget that allows you to click, pick a file, and have it
set the wallpaper to that file.
This is good, but every time it
loads it points to that same folder, what if you had multiple folders?
What if you want it to STORE what folder you picked the last time?
STEP 2 - Pulling the Folder name from the Open Window Dialog box
Lets add a
few lines of code to our script.
Our entire script looks like this
now:
Vbscript
Code
'Called when the script is executed
Sub Object_OnScriptEnter
End Sub
'Called when the script is terminated
Sub Object_OnScriptExit
End Sub
Function Object_OnLButtonUp(x, y, Dragged)
If Dragged = False Then
FolderName = "C:\wallpaper\"
wallpaper = System.FileOpenDialog("Select Wallpaper...", "", FolderName , "Wallpapers|*.jpg", 0)
System.SetWallpaper wallpaper, 3
End If
End Function
By Adding dim FolderName at the
very top of the script it will make that variable Global, meaning it will be
able to read/write to that var from any/all functions/subs.
If you don't add this it will not be able to pull the value of FolderName from
the other functions.
Vbscript
Code
Dim foldername
'Called when the script is executed
Sub Object_OnScriptEnter
End Sub
'Called when the script is terminated
Sub Object_OnScriptExit
End Sub
We are going to add a way to pull
the Folder name from the one we selected in our Dialog box.
To do this we need to "pull" the file name, and then strip that out from the
var.
We are going to assume that the
user clicked on another drive/folder/filename from the open dialog.
Let's say I picked: D:\My
data\Wallpapers\ZubazisUgly.jpg
Breaking this apart:
Set filesys = CreateObject("Scripting.FileSystemObject")
Sets the var FILESYS to a FileSystemObject
f = filesys.GetBaseName(wallpaper)
Pulls the File name from the entire path
In this case it pulls: ZubazisUgly.jpg
t = instr(1,wallpaper,f)
This finds the position of the file name in the path, so that we can
strip it out.
FolderName = left(wallpaper,t-1)
This pulls the LEFT T-1 characters
from the string so we end up with just the path: D:\My
data\Wallpapers\
Vbscript
Code
Function Object_OnLButtonUp(x, y, Dragged)
Dim filesys
If Dragged = False Then
FolderName = "C:\wallpaper\"
wallpaper = System.FileOpenDialog("Select Wallpaper...", "", FolderName , "Wallpapers|*.jpg", 0)
System.SetWallpaper wallpaper, 3
Set filesys = CreateObject("Scripting.FileSystemObject")
f = filesys.GetBaseName(wallpaper)
t = instr(1,wallpaper,f)
FolderName = left(wallpaper,t-1)
End If
End Function
This is all well and good, but
the next time we click the button it will still use the C:\wallpaper folder, so
we want to add a little more code to save and restore this var.
STEP 3 - Adding the Save / Load registry functions
We are going to add (2) new
functions to the script:
The SaveSettings and
LoadSettings are functions we talked about in
Tutorial #6.
We are storing the FolderName
into the reg key "HKCU\SOFTWARE\desktopx\wallpaperpicker\folder"
We use a DEFAULT of the
Current DesktopX Executable Directory so that it loads up using the folder
the EXE was run from.
You don't want to hard-code a folder here because not everyone's pc will
have that folder location.
Vbscript
Code
Function SaveSettings()
Set Sh = CreateObject("WScript.Shell")
Sh.RegWrite "HKCU\SOFTWARE\desktopx\wallpaperpicker\folder", foldername
Set Sh = Nothing
End Function
Function LoadSettings()
foldername = Desktopx.ExecutableDirectory
On Error Resume Next
Set Sh = CreateObject("WScript.Shell")
FolderName = Sh.RegRead("HKCU\SOFTWARE\desktopx\wallpaperpicker\folder")
Set Sh = Nothing
Err.Clear
End Function
STEP 4 - Putting it all together
Lets see what the entire script
looks like now, with all the above included.
Vbscript
Code
Dim foldername
'Called when the script is executed
Sub Object_OnScriptEnter
End Sub
'Called when the script is terminated
Sub Object_OnScriptExit
End Sub
Function Object_OnLButtonUp(x, y, Dragged)
Dim filesys
If Dragged = False Then
Call LoadSettings()
wallpaper = System.FileOpenDialog("Select Wallpaper...", "", FolderName , "Wallpapers|*.jpg", 0)
System.SetWallpaper wallpaper, 3
Set filesys = CreateObject("Scripting.FileSystemObject")
f = filesys.GetBaseName(wallpaper)
t = instr(1,wallpaper,f)
foldername = left(wallpaper,t-1)
Call SaveSettings()
End If
End Function
Function SaveSettings()
Set Sh = CreateObject("WScript.Shell")
Sh.RegWrite "HKCU\SOFTWARE\desktopx\wallpaperpicker\folder", foldername
Set Sh = Nothing
End Function
Function LoadSettings()
foldername = Desktopx.ExecutableDirectory
On Error Resume Next
Set Sh = CreateObject("WScript.Shell")
FolderName = Sh.RegRead("HKCU\SOFTWARE\desktopx\wallpaperpicker\folder")
Set Sh = Nothing
Err.Clear
End Function
This should allow you to click
and open a dialog box, allowing you to pick a folder/file to set the wallpaper,
then it sets the wallpaper, and stores the selected folder.
STEP 5 - Adding a Right-Click menu for options.
The next step is to add a menu so
we can pick from default/centered/tiled/stretched for the wallpaper.
I made a tutorial a while back on how to add a Right-click menu -
CLICK HERE
to view that, I'm not going to go thru this step by step, I'm just going to give
you the working code. If you want a break down of how the menu works,
please use the above link to the other Tutorial.
The code below will popup a menu
when you right-click. It gives you 4 options, from changing to the default
wallpaper, to making it tiled/stretched.
It will do this immediately so you can see what it does, it also stores the info
in a Var (WPFormat) so that the next time you pick a wallpaper it will use this
same setting. With 1 exception the 0. This is designed to set the
wallpaper to the default, so we don't want to STORE that, we will default it back
to 3 (stretched).
Vbscript
Code
Function Object_OnRButtonUpEx(obj,x,y,dragged)
If Not dragged Then
Object_OnRButtonUpEx = True
Set mainmenu = nothing
Set mainmenu = DesktopX.CreatePopupMenu
mainmenu.AppendMenu 0, 0, "Default Wallpaper"
mainmenu.AppendMenu 0, 1, "Center Wallpaper"
mainmenu.AppendMenu 0, 2, "Tile Wallpaper"
mainmenu.AppendMenu 0, 3, "Stretch Wallpaper"
result = mainmenu.TrackPopupMenu(0, System.CursorX, System.CursorY)
Select Case result
Case 0
WPFormat
= 3 System.SetWallpaper "",
0 Case 1
WPFormat = 1
System.SetWallpaper wallpaper, WPFormat
Case 2
WPFormat = 2
System.SetWallpaper wallpaper, WPFormat
Case 3
WPFormat = 3
System.SetWallpaper wallpaper, WPFormat
End Select
Set mainmenu = nothing
End If
End Function
STEP 6 - Putting it all together
We are going to add a few more
DIMs at the top:
Dim Wallpaper - stores the
selected Wallpaper name
Dim WPFormat - Stores the
Current Wallpaper "format" - stretched/centered/etc.
Here is the FULL script, if
you just want to copy/paste this into your object, its your call.
Vbscript
Code
Dim foldername
Dim Wallpaper
Dim WPFormat
'Called when the script is executed
Sub Object_OnScriptEnter
Call LoadSettings()
End Sub
'Called when the script is terminated
Sub Object_OnScriptExit
Call SaveSettings()
End Sub
Function Object_OnLButtonUp(x, y, Dragged)
Dim filesys
If Dragged = False Then
Call LoadSettings()
wallpaper = System.FileOpenDialog("Select Wallpaper...", "", FolderName , "Wallpapers|*.jpg", 0)
If len(wallpaper) > 2 Then
System.SetWallpaper wallpaper, WPFormat
Set filesys = CreateObject("Scripting.FileSystemObject")
f = filesys.GetBaseName(wallpaper)
t = instr(1,wallpaper,f)
foldername = left(wallpaper,t-1)
Call SaveSettings()
End If
End If
End Function
Function SaveSettings()
Set Sh = CreateObject("WScript.Shell")
Sh.RegWrite "HKCU\SOFTWARE\desktopx\wallpaperpicker\folder", foldername
Sh.RegWrite "HKCU\SOFTWARE\desktopx\wallpaperpicker\WPFormat", WPFormat
Set Sh = Nothing
End Function
Function LoadSettings()
foldername = Desktopx.ExecutableDirectory
WPFormat= 3 'use stretch for default (change to whatever you want)
On Error Resume Next
Set Sh = CreateObject("WScript.Shell")
FolderName = Sh.RegRead("HKCU\SOFTWARE\desktopx\wallpaperpicker\folder")
WPFormat = Sh.RegRead("HKCU\SOFTWARE\desktopx\wallpaperpicker\WPFormat")
Wallpaper = Sh.RegRead("HKEY_CURRENT_USER\Control Panel\Desktop\Wallpaper")
Set Sh = Nothing
Err.Clear
End Function
Function Object_OnRButtonUpEx(obj,x,y,dragged)
If Not dragged Then
Object_OnRButtonUpEx = True
Set mainmenu = nothing
Set mainmenu = DesktopX.CreatePopupMenu
mainmenu.AppendMenu 0, 0, "Default Wallpaper"
mainmenu.AppendMenu 0, 1, "Center Wallpaper"
mainmenu.AppendMenu 0, 2, "Tile Wallpaper"
mainmenu.AppendMenu 0, 3, "Stretch Wallpaper"
result = mainmenu.TrackPopupMenu(0, System.CursorX, System.CursorY)
Select Case result
Case 0
WPFormat = 0
System.SetWallpaper "", WPFormat
Case 1
WPFormat = 1
System.SetWallpaper wallpaper, WPFormat
Case 2
WPFormat = 2
System.SetWallpaper wallpaper, WPFormat
Case 3
WPFormat = 3
System.SetWallpaper wallpaper, WPFormat
End Select
Set mainmenu = nothing
End If
End Function
CONCLUSION
I hope you took the time to enter
the code (not just copy/pasted the entire thing) so you could work thru the
tutorial step-by-step and see how things work. You will notice I added a
small IF under the Open Dialog box so that it only does anything if you select a
file, it will crash out without that code if you don't select a file.
I hope you have enjoyed
this step into DX, and look forward to the next installment..